package com.eliza.music.ui.compoment

import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.eliza.library.ui.inter.IBaseRoute
import com.eliza.music.view.router.MusicRouter
import kotlinx.coroutines.launch


@Preview(showBackground = true, showSystemUi = true, backgroundColor = 0x00FB8C8)
@Composable
private fun LayoutPreview() {
    LayoutRootScreen(routes = MusicRouter.entries) { nav, build ->
        MusicRouter.RegisterRoute(nav, build)
    }
}

@Composable
fun LayoutRootScreen(
    nav: NavHostController = rememberNavController(),
    routes: List<IBaseRoute>,
    handle: (nav: NavHostController, build: NavGraphBuilder) -> Unit
) {
    val coroutineScope = rememberCoroutineScope()
    val pagerState = rememberPagerState { routes.size }

    Scaffold(
        topBar = {
            Text("snackbarHost")
        },
        bottomBar = {
            TabNavBar(
                pages = routes,
                pagerState = pagerState
            ) { index ->
                coroutineScope.launch {
//                    pagerState.animateScrollToPage(index)
                    nav.navigate(routes[index].title) {

                        launchSingleTop = true
                        restoreState = true
                    }
                }
            }
        },
    ) { paddingValues ->
        // 注册路由 显示组件
        NavHost(
            navController = nav,
            startDestination = routes[0].title,
            enterTransition = { EnterTransition.None },
            exitTransition = { ExitTransition.None },
            modifier = Modifier.padding(paddingValues)
        ) {
            handle(nav, this)
        }
    }
}

